﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 数据结构课设_电文的编码与译码_CSharp
{
    /// <summary>
    /// Tree链表类
    /// </summary>
    public class Link
    {
        public Link()//构造函数
        {
            //初始化
            ListCountValue = 0;
            Head = null;
            Tail = null;
        }

        private Node Head;//头指针
        private Node Tail;//尾指针
        private Node Current;//当前指针
        private int ListCountValue;//链表数据的个数

        /// <summary>
        /// 设置node的值
        /// </summary>
        public void SetNode(Node node) {
            Node temp_ndoe = node;
            while (temp_ndoe != null) {
                Append(temp_ndoe.Weight, temp_ndoe.Key);
                temp_ndoe = temp_ndoe.Next;
            }
            return;
        }

        /// <summary>
        /// 获取当前node
        /// </summary>
        public Node GetNode()
        {
            MoveFrist();
            return Current;
        }

        /// <summary>
        /// 向尾部添加node
        /// </summary>
        public void Append(ulong Weight , char DataValue)
        {
            Node NewNode = new Node(Weight,DataValue);

            if (IsNull())//如果头指针为空
            {
                Head = NewNode;
                Tail = NewNode;
            }
            else
            {
                Tail.Next = NewNode;
                NewNode.Previous = Tail;
                Tail = NewNode;
            }
            Current = NewNode;
            ListCountValue += 1;//链表个数加 1
        }

        /// <summary>
        /// 删除尾部node
        /// </summary>
        public void Delete()
        {
            if (!IsNull())//若不为空链表
            {
                if (!IsBof())//若删除头
                {
                    Head = Current.Next;
                    Current = Head;
                    ListCountValue -= 1;
                    return;
                }
                if (!IsEof())//若删除尾
                {
                    Tail = Current.Previous;
                    Current = Tail;
                    ListCountValue -= 1;
                    return;
                }
                Current.Previous.Next = Current.Next;//若删除中间数据
                Current = Current.Previous;
                ListCountValue -= 1;
                return;
            }
        }

        /// <summary>
        /// 向后移动一个node的方法
        /// </summary>
        public Boolean MoveNext()
        {
            if (!IsEof())
            {
                Current = Current.Next;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 向前移动一个node的方法
        /// </summary>
        public Boolean MovePrevious()
        {
            if (!IsBof())
            {
                Current = Current.Previous;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 移动到第一个node的方法
        /// </summary>
        public void MoveFrist()
        {
            Current = Head;
        }

        /// <summary>
        /// 移动到最后一个node的方法
        /// </summary>
        public void MoveLast()
        {
            Current = Tail;
        }

        /// <summary>
        /// 判断是否为空链表
        /// </summary>
        public bool IsNull()
        {
            if (ListCountValue == 0)
                return true;
            return false;
        }

        /// <summary>
        /// 判断是否为到达尾部node
        /// </summary>
        public bool IsEof()
        {
            if (Current == Tail)
                return true;
            return false;
        }

        /// <summary>
        /// 判断是否为到达头node
        /// </summary>
        public bool IsBof()
        {
            if (Current == Head)
                return true;
            return false;
        }

        /// <summary>
        /// 取得当前node的key
        /// </summary>
        public char GetCurrentKey()
        {
            return Current.Key;
        }

        /// <summary>
        /// 取得当前node的weight
        /// </summary>
        public ulong GetCurrentWeight()
        {
            return Current.Weight;
        }

        /// <summary>
        /// 取得链表的长度
        /// </summary>
        public int ListCount()
        {
            return ListCountValue;
        }

        /// <summary>
        /// 清空链表
        /// </summary>
        public void Clear()
        {
            MoveFrist();
            while (!IsNull())
            {
                Delete();//若不为空链表，从尾部删除
            }
        }

        /// <summary>
        /// 在当前位置前插入node
        /// </summary>
        public void Insert(ulong Weight, char DataValue)
        {
            //Console.WriteLine(Weight + "+" + GetCurrentWeight());
            Node NewNode = new Node(Weight,DataValue);
            if (IsNull())
            {
                Append(Weight, DataValue);//为空表，则添加
            }
            else if (IsBof())
            {
                //为头部插入
                NewNode.Next = Current;
                Current.Previous = NewNode;
                Head = NewNode;
                Current = Head;
                ListCountValue += 1;
                PrintLink();
            }
            else
            {
                //插入当前node前
                NewNode.Next = Current;
                NewNode.Previous = Current.Previous;
                Current.Previous.Next = NewNode;
                Current.Previous = NewNode;
                Current = NewNode;
                ListCountValue += 1;
                PrintLink();
            }
        }

        /// <summary>
        /// 进行Weight升序插入
        /// </summary>
        public void InsertAscending(ulong Weight , char InsertValue)
        {
            //参数：InsertValue 插入的数据
            //为空链表
            if (IsNull())
            {
                //添加
                Append(Weight,InsertValue);
                return;
            }
            
            MoveFrist();//移动到头
            while (true)
            {
                //Console.WriteLine(Weight+"+" + GetCurrentWeight());
                if (Weight < GetCurrentWeight())
                {
                    //满足条件，则插入，退出
                    Insert(Weight,InsertValue);
                    break;
                }
                //判断是否到达尾node
                if (IsEof())
                {
                    Append(Weight,InsertValue);
                    break;
                }
                //移动到下一个指针
                MoveNext();
            }
        }
        /// <summary>
        /// 遍历链表,内部测试方法
        /// </summary>
        public void PrintLink()
        {
            ulong i=0;
            Console.WriteLine("开始遍历:");
            Console.WriteLine("链表长度"+ ListCount());
            MoveFrist();
            do
            {
                i += GetCurrentWeight();
                //Console.WriteLine(((int)(GetCurrentKey())).ToString("x8") + " " + (GetCurrentWeight()).ToString());
            } while (MoveNext());
            Console.WriteLine("链表总权值" + i);
        }
    }
}
